package com.hoshiicloud.shxping.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.AESUtil;
import com.hoshiicloud.saleorder.dto.OrderDTO;
import com.hoshiicloud.shxping.feign.OrderFeignService;
import com.hoshiicloud.shxping.param.StoreOrderQueryParam;
import com.hoshiicloud.shxping.param.StoreVerificationUpdateParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ygd
 * @title: VerificationController
 * @projectName hoshiicloud-root
 * @description: 核销Controller
 * @date 2019/6/26
 */
@RestController
@Slf4j
@RequestMapping("/verification")
@Api(value = "核销接口", tags = {"核销操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class VerificationController {

    // 订单微服务Service
    @Resource
    private OrderFeignService orderFeignService;

    /**
     * 店铺核销
     *
     * @param param 核销参数
     * @return Response
     */
    @PostMapping(value = "/orderId")
    @ApiOperation(value = "店铺核销", notes = "店铺核销", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response storeVerification(@RequestBody StoreVerificationUpdateParam param) {

        if (param == null) {
            return Response.failed("非法请求");
        }

        try {
            //对称解密核销码
            String orderId = AESUtil.decrypt(param.getCode());

            //根据主订单id查询订单
            Response<OrderDTO> orderDTOResponse = orderFeignService.viewOrder(Long.parseLong(orderId));

            if (orderDTOResponse == null || null == orderDTOResponse.getData()) {
                return Response.failed("非法请求");
            }

            OrderDTO orderDTO = orderDTOResponse.getData();

            if (!orderDTO.getStoreId().equals(param.getStoreId())) {
                return Response.failed("核销失败");
            }
            return orderFeignService.verificationOrder(orderDTO.getOrderId());


        } catch (Exception e) {
            log.error("核销订单异常："+ e.getMessage());
            return Response.failed("核销订单异常："+ e.getMessage());
        }

    }

    /**
     * 批量核销订单
     * @param param 核销参数
     * @return Response
     */
    @PostMapping(value = "/storeIdAndMemberId")
    @ApiOperation(value = "批量核销订单", notes = "根据店铺ID和会员ID批量核销订单", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response VerificationOrderByStoreIdAndMemberId(@RequestBody StoreVerificationUpdateParam param) {
        try {
            if (null == param || null == param.getStoreId() || null == param.getCode()) {
                return Response.failed("非法请求,请求参数错误！");
            } else{
                StoreOrderQueryParam orderParam = new StoreOrderQueryParam();
                orderParam.setStatus(3);
                if (StringUtils.isNotEmpty(param.getPageNumber())) {
                    orderParam.setPageNumber(param.getPageNumber());
                }
                if (StringUtils.isNotEmpty(param.getPageSize())) {
                    orderParam.setPageSize(param.getPageSize());
                }
                orderParam.setStoreId(param.getStoreId());
                String decryptStr = AESUtil.decrypt(param.getCode());
                String[] arr = decryptStr.split(":");
                orderParam.setMemberId(Long.parseLong(arr[0]));
                Response<Page<OrderDTO>> rs = orderFeignService.listStoreOrder(orderParam);
                if (null != rs && rs.getCode() == 200 && null != rs.getData().getRecords()) {
                    rs.getData().getRecords().forEach(item -> {
                        orderFeignService.verificationOrder(item.getOrderId());
                    });
                }
                return Response.success("批量核销成功！");
            }
        } catch (Exception e) {
            log.error("批量核销订单异常："+ e.getMessage());
            return Response.failed("批量核销订单异常："+ e.getMessage());
        }
    }

}
